import { createRouter, createWebHashHistory } from 'vue-router'

import HomeView from '@/views/HomeView/index.vue'
import WelcomeView from '@/views/WelcomeView.vue'
import AboutView from '@/views/AboutView.vue'
import PostView from '@/views/PostView.vue'

import pinia from '@/stores'
import useViewStore from '@/stores/view.js'
const viewStore = useViewStore(pinia)

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/welcome',
      name: 'welcome',
      component: WelcomeView
    },
    {
      path: '/about',
      name: 'about',
      component: AboutView
      // component: () => import('@/views/AboutView.vue')
    },
    {
      path: '/post',
      name: 'post',
      component: PostView
      // component: () => import('@/views/AboutView.vue')
    },
    {
      path: '/user',
      name: 'user',
      // component: UserView
      component: () => import('@/views/UserView/index.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'welcome' && viewStore.office == 0) {
    next({ name: 'welcome' })
  } else if (to.name == 'welcome' && viewStore.office != 0) {
    next(from)
  } else {
    next()
  }
})

export default router
